import {Tools,Toast} from 'weaver-mobile';
const getLabel = Tools.getLabel;
const DocConstant = {
  catchApiError: (result) => {
    return new Promise((resolve, reject) => {
      // console.log("result",result);
      // console.log("result.api_status",result.api_status);
      if (!result.api_status) {
        Toast.fail(result.msg || getLabel(30651, '操作失败'), 1, () => {
          reject(result);
        });
        return;
      }
      resolve(result);
    });
  },
  // 通过这个判断有没有分享之类的功能。
  isApp: () => {
    return !!window.em;
  },
  getEcId: () => {
    return window.localStorage.emobile_ec_id;
  },
  getLocationParams: () => {
    let url = window.location.href;
    if (url.indexOf("?") < 0) {
      return null;
    }
    let urlParams = url.slice(url.indexOf('?') + 1).split('&');
    let params = [];
    urlParams.forEach(element => {
      let index = element.indexOf('=');
      let _key =element.slice(0,index);
      let _value =element.slice(index+1);
      params[_key] = _value;
    });
    return params;
  },
  //拿到问号之后的所有参数
  getLocationUrlParams: () =>{
    let url = window.location.href;
    if (url.indexOf("?") < 0) {
      return null;
    }
    let urlParams = url.slice(url.indexOf('?') + 1);
    return urlParams;
  }
}

export default DocConstant;
